import { User } from '@/api/interface';
import { customTable } from '@/components/ProTable/interface';
import { ProTableValue } from '@/interface/public';

import { formOpen } from '@/utils/form';
import { useAddForm } from './addForm';
import { useChangePwdForm } from './changePwd';

export function useDataGrid(proTableBox: ProTableValue) {
  const { addForm, saveAddForm } = useAddForm(proTableBox);
  const { changePwdForm, savePwdForm } = useChangePwdForm(proTableBox);
  let dataGrid = reactive<customTable>({
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'left', width: 270 },
      {
        prop: 'username',
        label: 'username',
        search: {
          el: 'input',
          key: 'key',
          label: 'keyValue',
          props: {
            placeholder: 'userPermissions_key'
          }
        }
      },
      {
        prop: 'nick',
        label: 'name'
      },

      {
        prop: 'status',
        label: '用户状态',
        tag: true,
        enum: statusEnableOrDisable
      },
      {
        prop: 'remark',
        label: 'remark'
      }
    ],
    headButtonList: [
      {
        label: 'addNewUsers',
        type: 'primary',
        icon: 'Plus',
        // permissionCode: 'p:auth:user:save',
        methods: async () => {
          formChangeItemStatus(addForm, ['password'], false, 'hide');
          formChangeItemStatus(addForm, ['username'], false);
          formOpen(addForm, 'addNewUsers');
        }
      }
    ],
    rowButtonList: [
      {
        label: 'edit',
        type: 'primary',
        // permissionCode: 'p:auth:user:save',
        methods: row => {
          formChangeItemStatus(addForm, ['password'], true, 'hide');
          formChangeItemStatus(addForm, ['username'], true);
          formOpen(addForm, 'editUsers', row);
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'changePassword',
        type: 'primary',
        // permissionCode: 'p:auth:user:upPwd',
        methods: row => {
          const { id, username } = row;
          const params = {
            username,
            id
          };
          formOpen(changePwdForm, 'changePassword', params);
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'enable',
        type: 'primary',
        // permissionCode: 'p:auth:user:status',
        display: item => item.status === 1,
        methods: row => {
          const params = { id: row.id, status: 0 };
          useHandleData(
            userEnable,
            params,
            `${transitionLanguages('table', 'disabled')}【${row.username}】${transitionLanguages('table', 'user')}`,
            proTableBox.value?.getTableList
          );
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'disabled',
        type: 'danger',
        // permissionCode: 'p:auth:user:status',
        display: item => item.status === 0,
        methods: row => {
          const params = { id: row.id, status: 1 };
          useHandleData(
            userDisable,
            params,
            `${transitionLanguages('table', 'disabled')}【${row.username}】${transitionLanguages('table', 'user')}`,
            proTableBox.value?.getTableList
          );
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  return { dataGrid, changePwdForm, addForm, saveAddForm, savePwdForm };
}
